using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class ScoreDAL
    {
        public static int Save(ScoreInfo info)
        {
            string sql = string.Format("insert into [score]([AdminID],[UserID],[Score],[Type],[Bak],[CreateDate]) values(@AdminID,@UserID,@Score,@Type,@Bak,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@Score", info.Score),
                new SqlParameter("@Type", info.Type),
                new SqlParameter("@Bak", info.Bak),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(ScoreInfo info)
        {
            string sql = string.Format("update [score] set [AdminID]=@AdminID,[Score]=@Score,[Type]=@Type,[Bak]=@Bak where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@Score", info.Score),
                new SqlParameter("@Type", info.Type),
                new SqlParameter("@Bak", info.Bak),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static ScoreInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[score]");
            return SimpleData(dr);
        }
        public static List<ScoreInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[score]");
            return ListSimpleData(dt);
        }
        public static List<ScoreInfo> FindPage(int userid, int type, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            filter.AppendFormat(" and [score].UserID={0}", userid);
            filter.AppendFormat(" and [score].Type={0}", type);
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[score]", out count);
            return ListSimpleData(dt);
        }
        public static List<ScoreInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [score]"));
            return ListSimpleData(dt);
        }
        private static ScoreInfo SimpleData(DataRow dr)
        {
            ScoreInfo info = new ScoreInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Score = DataConvert.ToInt(dr["Score"]);
                info.Type = DataConvert.ToInt(dr["Type"]);
                info.Bak = DataConvert.ToString(dr["Bak"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateText = info.CreateDate.ToString("yyyy-MM-dd");
            }
            return info;
        }
        private static ScoreInfo AllData(DataRow dr)
        {
            ScoreInfo info = new ScoreInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.Score = DataConvert.ToInt(dr["Score"]);
                info.Type = DataConvert.ToInt(dr["Type"]);
                info.Bak = DataConvert.ToString(dr["Bak"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
        private static List<ScoreInfo> ListSimpleData(DataTable dt)
        {
            List<ScoreInfo> list = new List<ScoreInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<ScoreInfo> ListAllData(DataTable dt)
        {
            List<ScoreInfo> list = new List<ScoreInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

